// pages/crowd/index/index.js
var common = require("../../../public.js");
var that
Page({
  data: {
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    crowdInfo: {},
    showPop: false,
    currentCrowd: 0,
    richText: '',
    count: 1,
    favoriteGoodsFlag: false,
    over: false,  //超出金额
    swiper_current: 0,
    tabs_active: 0,
    scroll_into_view: '',
    indicatorDots: false,
    autoplay: false,
    interval: 2000,
  },
  onLoad: function (options) {
    let capsule = wx.getMenuButtonBoundingClientRect()
    this.setData({
      height: capsule.height,
      top: capsule.top,
      storeId: options.id,

    })
    that = this
  
		if (options.q){
			let scan_url = decodeURIComponent(options.q);
			let video_id = scan_url.match(/\d+/) //提取链接中的数字，也就是链接中的参数id，/\d+/ 为正则表达式s
			common.getLocalUserInfo(function (success, userInfo) {
			  if (success) {
				getApp().getLocation(function (data) {
				  that.setData({
					
          crowdId:video_id[0],
					userInfo: userInfo.user,
					token: userInfo.token,
					openid: userInfo.openid
				  }, function () {
            that.getCrowdDetail()
				  })
				})
			  }else{
				common.showLoginPage()
			  }
			}, true)
			 return
			}else{
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          crowdId: options.id,
          openid: res.data.openid
        }, () => {
          that.getCrowdDetail()
          // that.findList()
        })
      },
      fail() {
        common.showLoginPage()
      }
    })
  }
  },
  // 轮播图滚动
  swiperChange(e) {
    this.setData({
      swiper_current: e.detail.current
    })
  },
  // tabs改变
  tabsChange(e) {
    let index = e.detail.index;
    if (index == 0) {
      this.setData({
        scroll_into_view: 'goodsTitle',
      })
    } else if (index == 1) {
      this.setData({
        scroll_into_view: 'rule',
      })
    } else if (index == 2) {
      this.setData({
        scroll_into_view: 'invitation',
      })
    }

    // console.log(this.data.scroll_into_view)
  },
  // 库存判断
  click_money(e) {
    console.log(e.detail)
  },
  //跳转上一个页面
  goBack() {
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; //上一个页面
    // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
    if (prevPage) {
      wx.navigateBack({
        delta: 1
      })
    } else {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }
  },
  //查询是否收藏商品状态
  getgoodsArrtention: function () {
    const that = this
    const tempJson = {
      token: that.data.token,
      userId: that.data.userInfo.id,
      crowdfundingId: that.data.crowdInfo.id
    }
    common.post("/crowdfunding/getCollection", tempJson, function (res) {
      if (res.status == 200) {
        that.setData({
          favoriteGoodsFlag: true
        })
      } else {
        that.setData({
          favoriteGoodsFlag: false
        })
      }
    })
  },
  // 收藏
  collect() {
    if (this.data.favoriteGoodsFlag) {
      this.favoriteGoods(false)
    } else {
      this.favoriteGoods(true)
    }
  },
  // 收藏
  favoriteGoods(collect) {
    const that = this
    const tempJson = {
      token: that.data.token,
      userId: that.data.userInfo.id,
      crowdfundingId: that.data.crowdInfo.id
    }
    const url = collect ? '/crowdfunding/addCollection' : '/crowdfunding/closeCollection'
    common.post(url, tempJson, function (res) {
      if (res.status == 200) {
        that.setData({
          favoriteGoodsFlag: collect
        })
        common.showToast(collect ? '已收藏' : '已取消收藏')
      } else {
        common.showToast(collect ? '收藏失败' : '取消失败')
      }
    })
  },

  getCrowdDetail() {
    let tempJson = {
      crowdfundingId: that.data.crowdId
      // crowdfundingId: 131
    }
    common.post("/crowdfunding/getDetail", tempJson, function (res) {
      if (res.data.goodsDetail) {
        let richText = res.data.goodsDetail;
        // const regex = new RegExp('<img');
        richText = richText.replace(/\<img/gi, '<img class="rich-img" ');
        that.setData({
          crowdInfo: res.data,
          richText: richText
        }, _ => {
          that.getStoreInfo()
          that.getgoodsArrtention()
        })
        return
      }
      that.setData({
        crowdInfo: res.data,
        richText: ''
      }, _ => {
        that.getStoreInfo()
        that.getgoodsArrtention()
      })
    })
  },

  // 获取项目进度
  findList() {
    let tempJson = {
      crowdfundingId: that.data.crowdId
    }
    common.post("/crowdfundingProgress/findList", tempJson, function (res) {
      if (res.status == 200) {
        console.log(res.data)
      }
    })
  },
  getStoreInfo() {
    let tempJson = {
      storeId: that.data.crowdInfo.storeId,
      userId: that.data.userInfo.id,
      token: that.data.token
    }
    common.post("/store/info", tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          storeInfo: res.data
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },

  toStore(e) {
    common.gotoStore(this.data.storeInfo)
  },
  click_index() {
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  bookNow() {
    this.setData({
      showPop: true
    })
  },
  onStepChange(e) {
    console.log(e)
    this.setData({
      count: e.detail
    }, () => {
      if (this.data.count > this.data.crowdInfo.setmeal[this.data.currentCrowd].stock) {
        common.showToast('已超出库存')
        this.setData({
          over: true
        })
      } else {
        this.setData({
          over: false
        })
      }
    })
  },
  chooseType(e) {
    console.log(e)
    let {
      index
    } = e.currentTarget.dataset
    this.setData({
      currentCrowd: index
    })
  },
  onClose() {
    this.setData({
      showPop: false
    })
  },
  toOrder() {
    if (this.data.over) {
      common.showToast('超出库存')
      return
    }
    let params = {
      crowdfundingId: this.data.crowdInfo.id,
      setmeal: this.data.crowdInfo.setmeal[this.data.currentCrowd],
      payNum: this.data.count,
      storeId: this.data.storeInfo.id,
      storeName: this.data.name,
      storeImage: this.data.storeImage
    }
    console.log(params, "45555555")
    wx.navigateTo({
      url: '/pages/crowd/crowdOrder/index?params=' + JSON.stringify(params),
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      return {
        title: that.data.crowdInfo.title,
        path: '/pages/crowd/index/index?id=' + this.data.crowdId,
        success: function (res) {
          // console.log('成功', res)
        }
      }
    }
  },
})